package com.aliyun.messagemgt.repository;

import com.aliyun.messagemgt.common.annotation.PagingAnnotation;
import com.aliyun.messagemgt.converter.BaseReceiveGroupConverter;
import com.aliyun.messagemgt.domain.repository.IBaseReceiveGroupRepository;
import com.aliyun.messagemgt.dto.base.BaseReceiveGroupAddCmd;
import com.aliyun.messagemgt.dto.base.BaseReceiveGroupDTO;
import com.aliyun.messagemgt.dto.base.BaseReceiveGroupQry;
import com.aliyun.messagemgt.dto.base.BaseReceiveGroupUpdateCmd;
import com.aliyun.messagemgt.entities.BaseReceiveGroup;
import com.aliyun.messagemgt.repository.mappers.BaseReceiveGroupMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 基础表-接收组表 服务实现类
 * </p>
 *
 * @author wugongzi
 * @since 2021-09-28
 */
@Service
public class BaseReceiveGroupMybatisRepository implements IBaseReceiveGroupRepository {

    @Autowired
    private BaseReceiveGroupMapper baseReceiveGroupMapper;

    @Override
    @PagingAnnotation
    public PageInfo<BaseReceiveGroupDTO> page(BaseReceiveGroupQry baseReceiveGroupQry) {
        PageHelper.orderBy("id desc");
        List<BaseReceiveGroup> baseReceiveGroupList = baseReceiveGroupMapper.page(baseReceiveGroupQry);
        PageInfo<BaseReceiveGroup> baseReceiveGroupPageInfo = new PageInfo<>(baseReceiveGroupList);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupPageInfo2baseReceiveGroupDTOPageInfo(baseReceiveGroupPageInfo);
    }

    @Override
    public int deleteById(Long id) {
        return baseReceiveGroupMapper.deleteById(id);
    }

    @Override
    public int update(BaseReceiveGroupUpdateCmd cmd) {
        BaseReceiveGroup baseReceiveGroup = BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupUpdateCmd2baseReceiveGroup(cmd);
        return baseReceiveGroupMapper.updateById(baseReceiveGroup);
    }

    @Override
    public void add(BaseReceiveGroupAddCmd cmd) {
        BaseReceiveGroup baseReceiveGroup = BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupAddCmd2baseReceiveGroup(cmd);
        int insert = baseReceiveGroupMapper.insert(baseReceiveGroup);
        cmd.setId(baseReceiveGroup.getId());
    }

    @Override
    public BaseReceiveGroupDTO get(Long id) {
        QueryWrapper<BaseReceiveGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        queryWrapper.eq("is_deleted", "N");
        BaseReceiveGroup baseReceiveGroup = baseReceiveGroupMapper.selectOne(queryWrapper);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroup2baseReceiveGroupDTO(baseReceiveGroup);
    }

    @Override
    public List<BaseReceiveGroupDTO> selectList(String tenantCode, List<String> receiveGroupCodes, String isEnabled) {
        QueryWrapper<BaseReceiveGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_code", tenantCode);
        queryWrapper.eq("is_enabled", isEnabled);
        queryWrapper.eq("is_deleted", "N");
        queryWrapper.in("receive_group_code", receiveGroupCodes);
        List<BaseReceiveGroup> baseReceiveGroupList = baseReceiveGroupMapper.selectList(queryWrapper);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupList2baseReceiveGroupDTOList(baseReceiveGroupList);
    }

    @Override
    public BaseReceiveGroupDTO getByReceiveGroupCode(String receiveGroupCode) {
        QueryWrapper<BaseReceiveGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("receive_group_code", receiveGroupCode);
        queryWrapper.eq("is_deleted", "N");
        queryWrapper.eq("is_enabled", "Y");
        BaseReceiveGroup baseReceiveGroup = baseReceiveGroupMapper.selectOne(queryWrapper);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroup2baseReceiveGroupDTO(baseReceiveGroup);
    }

    @Override
    public BaseReceiveGroupDTO get(BaseReceiveGroupQry qry) {
        BaseReceiveGroup baseReceiveGroupQry = BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupQry2baseReceiveGroup(qry);
        QueryWrapper<BaseReceiveGroup> queryWrapper = new QueryWrapper<>(baseReceiveGroupQry);
        BaseReceiveGroup baseReceiveGroup = baseReceiveGroupMapper.selectOne(queryWrapper);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroup2baseReceiveGroupDTO(baseReceiveGroup);
    }

    @Override
    public List<BaseReceiveGroupDTO> getList(BaseReceiveGroupQry qry) {
        BaseReceiveGroup baseReceiveGroupQry = BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupQry2baseReceiveGroup(qry);
        QueryWrapper<BaseReceiveGroup> queryWrapper = new QueryWrapper<>(baseReceiveGroupQry);
        List<BaseReceiveGroup> baseReceiveGroupList = baseReceiveGroupMapper.selectList(queryWrapper);
        return BaseReceiveGroupConverter.INSTANCE.baseReceiveGroupList2baseReceiveGroupDTOList(baseReceiveGroupList);
    }
}
